/**
 * 
 */
package model;

/**
 * Recipe ingredient information.
 * 
 * @author Matt Adams
 */
public class RecipeIngredient { 
    private Double my_quantity;
    private String my_prepStyle;
    private String my_measureUnit;
    private String my_ingredient;
    
    public RecipeIngredient(Double the_quantity, String the_prepStyle, String the_measureUnit, String the_ingredient) {
        my_quantity = the_quantity;
        my_prepStyle = the_prepStyle;
        my_measureUnit = the_measureUnit;
        my_ingredient = the_ingredient;
    }
    public Double getQuantity() {
        return my_quantity;
    }
    public String getPrepStyle() {
        return my_prepStyle;
    }
    public String getMeasureUnit() {
        return my_measureUnit;
    }
    public String getIngredient() {
        return my_ingredient;
    }
    /**
     * Produces and returns string representation of this ingredient.
     * @return Returns string representation of this ingredient.
     */
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (my_quantity != null && my_quantity != 0) {
            sb.append(toFraction(my_quantity, 10));
            sb.append(" ");
        }
        if (my_measureUnit != null && !my_measureUnit.isEmpty()) {
            sb.append(my_measureUnit);
            sb.append(" ");
        }
        if (my_ingredient != null && !my_ingredient.isEmpty()) {
            sb.append(my_ingredient);
            sb.append(" ");
        }
        if (my_prepStyle != null && !my_prepStyle.isEmpty()) {
            sb.append(my_prepStyle);
        }
        return sb.toString();
    }
    private String toFraction(double d, int factor) {
        StringBuilder sb = new StringBuilder();
        if (d < 0) {
            sb.append('-');
            d = -d;
        }
        long l = (long) d;
        if (l != 0) sb.append(l);
        d -= l;
        double error = Math.abs(d);
        int bestDenominator = 1;
        for(int i=2;i<=factor;i++) {
            double error2 = Math.abs(d - (double) Math.round(d * i) / i);
            if (error2 < error) {
                error = error2;
                bestDenominator = i;
            }
        }
        if (bestDenominator > 1)
            sb.append(' ').append(Math.round(d * bestDenominator)).append('/') .append(bestDenominator);
        return sb.toString();
    }
    /*private String toMixedFraction(double the_x) {
        String result = "";
        int denominator = (int) Math.round(1 / the_x);
        if (denominator > 0) {
            int w = (int) the_x,
                    n = (int) the_x * denominator % denominator,
                    a = n & -n;
            if (w > 0) {
                result = w + (n == 0 ? "" : " " + n / a + "/" + denominator / a);
            } else {
                result = (n == 0 ? "" : n / a + "/" + denominator / a);
            }
        } else {
            result += (int) the_x;
        }
        return result;
    }*/
}
